package com.其它.策略模式;



import com.lims.common.ResultCode;
import com.lims.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
@Slf4j
public class TestFactory implements ApplicationContextAware {


    private static final Map<String, ITestAccess> IMPL_MAP = new HashMap<>();

    public static ITestAccess getBean(String gfType) {
        ITestAccess syncAccess = IMPL_MAP.get(gfType);
        if(syncAccess == null){
            log.error("类型不支持{}", gfType);
            throw new BaseException(ResultCode.COMM_ERROR,"类型不支持");
        }
        return syncAccess;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ITestAccess> map = applicationContext.getBeansOfType(ITestAccess.class);
        map.forEach((key, value) -> IMPL_MAP.put(value.gfType(), value));
    }
}
